import { Entity, Column, ManyToOne, JoinColumn, OneToMany } from "typeorm";

import {
  User,
  Book,
  Media,
  TonSku,
  BookMap,
  TonOrder,
  OrderStock,
  INTEGER_PG,
  VARCHAR50_PG,
  VARCHAR255_PG,
  StockNodeLink,
  TonOrderGroup,
  ENUM_TON_ORDER,
  RefTonOrderMedia,
  RefTonOrderStamp,
  TonOrderGroupGateway,
  TonOrderMediaGateway,
  TonOrderStampGateway,
} from "qqlx-core";
import { TonOrderGateway } from "qqlx-core";

import { TransformerInteger } from "@lib/orm.transfor.number";
import { TransformerVarchar50, TransformerVarchar255 } from "@lib/orm.transfor.string";
import { TransformerEnum } from "@lib/orm.transfor.enum";

import { OrderSchema } from "../alpha/ext.com.order";
import { IdIntroSchema } from "@/alpha/ext.id.intro";
import { OwnerIdSchema } from "@/alpha/ext.id.base";

@Entity({ name: TonOrderGroupGateway.relations })
export class TonOrderGroupSchema extends IdIntroSchema implements TonOrderGroup {
  @OneToMany("TonOrderSchema", (s: TonOrder) => s.justTonOrderGroup)
  joinTonOrderList!: TonOrder[];

  @OneToMany("TonSkuSchema", (s: TonSku) => s.justTonOrderGroup)
  joinTonSkuList!: TonSku[];

  @OneToMany("StockNodeLinkSchema", (s: StockNodeLink) => s.justTonOrderGroup)
  joinStockNodeLinkList!: StockNodeLink[];
}

@Entity({ name: TonOrderGateway.relations })
export class TonOrderSchema extends OrderSchema implements TonOrder {
  @Column({ transformer: new TransformerEnum(Object.values(ENUM_TON_ORDER), ENUM_TON_ORDER.NONE) })
  type: ENUM_TON_ORDER = ENUM_TON_ORDER.NONE;

  @Column({ transformer: new TransformerVarchar255() })
  labelStr: VARCHAR255_PG = "";

  @Column({ transformer: new TransformerInteger() })
  refTonOrderGroupId!: INTEGER_PG;

  @ManyToOne("TonOrderGroupSchema", (s: TonOrderGroup) => s.joinTonOrderList)
  @JoinColumn({ name: "refTonOrderGroupId", referencedColumnName: "id" })
  justTonOrderGroup!: TonOrderGroup;

  //

  @OneToMany("RefTonOrderMediaSchema", (s: RefTonOrderMedia) => s.justTonOrder)
  joinRefTonOrderMediaList!: RefTonOrderMedia[];

  @OneToMany("RefTonOrderStampSchema", (s: RefTonOrderStamp) => s.justTonOrder)
  joinRefTonOrderStampList!: RefTonOrderStamp[];

  @OneToMany("TonSkuSchema", (s: TonSku) => s.justTonOrder)
  joinTonSkuList!: TonSku[];

  // ============ Join ============

  @OneToMany("BookSchema", (s: Book) => s.justTonOrder)
  joinBookList!: Book[];

  @OneToMany("BookMapSchema", (s: BookMap) => s.justTonOrder)
  joinBookMapList!: BookMap[];

  // ============ Join ============

  @OneToMany("OrderStockSchema", (s: OrderStock) => s.justTonOrder)
  joinOrderStockList!: OrderStock[];
}

@Entity({ name: TonOrderMediaGateway.relations })
export class RefTonOrderMediaSchema extends OwnerIdSchema implements RefTonOrderMedia {
  @Column({ transformer: new TransformerVarchar50() })
  refTonOrderId!: VARCHAR50_PG;

  @ManyToOne("TonOrderSchema", (e: TonOrder) => e.joinRefTonOrderMediaList)
  @JoinColumn({ name: "refTonOrderId", referencedColumnName: "uuid32" })
  justTonOrder!: TonOrder;

  @Column({ transformer: new TransformerInteger() })
  refMediaId!: INTEGER_PG;

  @ManyToOne("MediaSchema", (e: Media) => e.joinRefTonOrderMediaList)
  @JoinColumn({ name: "refMediaId", referencedColumnName: "id" })
  justMedia!: Media;
}

@Entity({ name: TonOrderStampGateway.relations })
export class RefTonOrderStampSchema extends IdIntroSchema implements RefTonOrderStamp {
  @Column({ transformer: new TransformerVarchar50() })
  refCreatorId!: VARCHAR50_PG;

  @ManyToOne("UserSchema")
  @JoinColumn({ name: "refCreatorId", referencedColumnName: "uuid32" })
  justCreator!: User;

  @Column({ transformer: new TransformerVarchar50() })
  refTonOrderId!: VARCHAR50_PG;

  @ManyToOne("TonOrderSchema", (e: TonOrder) => e.joinRefTonOrderStampList)
  @JoinColumn({ name: "refTonOrderId", referencedColumnName: "uuid32" })
  justTonOrder!: TonOrder;
}
